/**
 * 请假列表组件-教师端
 * Created by xxx on 2017/7/13.
 */
'use strict';
import React, {
    Component
} from 'react'
import {
    Link
} from 'react-router'
import {
    HttpService,
    dateStrFormat,
    dateFormat,
    Tips,
    getClassList,
    getDefaultImg,
    getLocalTime
} from '../Tools'

import Header from './Common/Header'
import LoadingBox from './Common/LoadingBox'

import ImgRightBlue from '../Img/icon/right_blue.png'

export default class ScoreDetail extends Component {
    constructor(props) {
        super(props);
        this.allDataList = [];
        this.state = {
            dataList: [],
            classList: [],
            classid: '',
            dataLoad: 1,
            dataNull: 0
        }
    }
    componentWillMount() {
        if (!sessionStorage.leavetypeclassid)
            sessionStorage.leavetypeclassid = ''
        if (!sessionStorage.leavetypeclassname)
            sessionStorage.leavetypeclassname = ''
        if (!sessionStorage.leavelisttype)
            sessionStorage.leavelisttype = 0
        if (sessionStorage.classid) {
            sessionStorage.leavetypeclassid = sessionStorage.classid
        } else {
            getClassList(classList => {
                if (sessionStorage.leavetypeclassid == '')
                    sessionStorage.leavetypeclassid = classList[0].id
                if (sessionStorage.leavetypeclassname == '')
                    sessionStorage.leavetypeclassname = classList[0].name
                this.setState({
                    classList
                });
            })
        }
        this.setState({
            classid: sessionStorage.leavetypeclassid
        });
        this.getDataList();
    }

    componentDidMount() {
        if (document.getElementById('leavetypecls'))
            document.getElementById('leavetypecls').value = sessionStorage.leavetypeclassid
    }


    //获取请假列表
    getDataList() {
        if (sessionStorage.orgid == '') {
            Tips('未获取到学校ID');
            return;
        }
        this.setState({
            dataLoad: 1,
            dataNull: 0
        });
        let url = '/jxt-bbzx/SickFormServlet';
        let data = {
            userid: sessionStorage.userid,
            schoolid: sessionStorage.orgid
        }
        if (sessionStorage.userrole != '家长') {
            data.classid = sessionStorage.leavetypeclassid
            url = '/jxt-bbzx/SickFormTeacherServlet'
        } else {
            data.childname = sessionStorage.username;
        }
        HttpService.query({
            url,
            data,
            success: (res => {
                if (res.list.item) {
                    let dataList = res.list.item
                    if (dataList instanceof Array == false) {
                        dataList = [];
                        dataList.push(res.list.item);
                    }
                    this.allDataList = dataList;
                    this.leaveType(sessionStorage.leavelisttype, 0);
                    this.setState({
                        dataLoad: 0
                    });
                    return;
                } else {
                    this.allDataList = [];
                    this.setState({
                        dataLoad: 0,
                        dataNull: 1,
                        dataList: []
                    });
                }
            }),
            fail:(msg=>{
                this.setState({
                    dataLoad: 0,
                    dataNull: 1
                });
            })
        })
    }

    //选择班级
    sltClass(e) {
        let obj = e.currentTarget
        const clsName = obj.options[obj.selectedIndex].text
        sessionStorage.leavetypeclassname = clsName
        sessionStorage.leavetypeclassid = obj.value
        document.querySelector('.h_title span').innerHTML = clsName
        this.setState({
            dataLoad: 1,
            dataNull: 0,
            classid: obj.value
        })
        this.getDataList();
    }
    //选择类型
    leaveType(index, type) {
        if (type == 1) {
            sessionStorage.leavelisttype = index;
            document.querySelector('#levtype .select').className = ''
            document.getElementById('levtype').children[index].className = 'select'
        }
        let newDataList = [];
        if (sessionStorage.userrole != '家长') {
            this.allDataList.map((item, index) => {
                if (item.check == undefined)
                    item.check = 0
                if (item.check == sessionStorage.leavelisttype)
                    newDataList.push(item)
            })
        } else {
            newDataList = this.allDataList
        }

        this.setState({
            dataLoad: 0,
            dataNull: newDataList.length == 0 ?
                1 : 0,
            dataEnd: 0,
            dataList: newDataList
        })
    }

    render() {
        const {
            dataLoad,
            dataNull,
            dataEnd,
            dataList,
            classList,
            classid
        } = this.state
        const dataTagList = dataList.map((item, index) => {
            let status = '审批中'
            if (item.check == 1)
                status = '同意'
            if (item.check == 2)
                status = '不同意'
            let name = item.name
            if (name == 'null' || name == 'undefined')
                name = '[无用户名]'
            return (
                <Link id='pjbleavelist' className="content_list clearfix" to={'/LeaveDetail/' + item.id + '/' + encodeURIComponent(item.name) + '/' + encodeURIComponent(item.type) + '/' + encodeURIComponent(dateStrFormat(item.from)) + '/' + encodeURIComponent(dateStrFormat(item.to)) + '/' + encodeURIComponent(status) + '/' + encodeURIComponent(item.content
                    ? item.content
                    : '无') + '/' + encodeURIComponent(item.content_t
                    ? item.content_t
                    : '无')}>
                    <div className="img">
                        <img src={getDefaultImg()}/>
                    </div>
                    <div className="info">
                        <div className="title">{name}</div>
                        <div className="info_list">
                            <span className="list_left" id='leavetitle'>请假类型：</span>
                            <span className="list_right">{item.type}</span>
                        </div>
                        <div className="info_list">
                            <span className="list_left" id='leavetitle'>请假时间：</span>
                            <span className="list_right">{dateStrFormat(item.from)}-<br/>{dateStrFormat(item.to)}</span>
                        </div>
                        {sessionStorage.userrole == '家长'
                            ? <div className="info_time">{getLocalTime(item.time.replace(/\./g,'/'))}</div>
                            : ''}
                        <span className={"type type" + item.check}>{status}
                            <img src={ImgRightBlue}/></span>
                    </div>
                </Link>
            )
        })
        const classTagList = classList.map((item, index) => {
            return (
                <option value={item.id}>{item.name}</option>
            )
        })
        let publishLink = null;
        if (sessionStorage.userrole == '家长')
            publishLink = <Link className='h_link add_blue' to={'/ApplyLeave/' + sessionStorage.leavetypeclassid}/>
        return (
            <div className='leave_list_page'>
                <Header link={publishLink} title={sessionStorage.classid
                    ? '请假信息'
                    : null} appLink='1'>
                    <span>{sessionStorage.leavetypeclassname}</span><br/> {classTagList != ''
                        ? <select dir="rtl" className="sltbox" id='leavetypecls' onChange={this.sltClass.bind(this)}>
                                {classTagList}
                            </select>
                        : ''}
                </Header>
                {sessionStorage.userrole != '家长'
                    ? <div className="type_list" id='levtype'>
                            <div onClick={this.leaveType.bind(this, 0, 1)} className={sessionStorage.leavelisttype==0?'select':''}>审批中</div>
                            <div onClick={this.leaveType.bind(this, 1, 1)} className={sessionStorage.leavelisttype==1?'select':''}  style={{
                                borderRight: '1px solid #01B4EE',
                                borderLeft: '1px solid #01B4EE'
                            }}>同意</div>
                        <div onClick={this.leaveType.bind(this, 2, 1)} className={sessionStorage.leavelisttype==2?'select':''}>不同意</div>
                        </div>
                    : ''}
                <div className="leave_list">
                    <LoadingBox dataLoad={dataLoad} dataNull={dataNull} dataEnd={dataEnd}/> {dataTagList}
                </div>
            </div>
        )
    }
}
